﻿// ***********************************************************************
// Assembly         : DataLayer
// Author           : LJO+01 (Laurent Jordi)
// Created          : 04-17-2013
//
// Last Modified By : LJO+01 (Laurent Jordi)
// Last Modified On : 06-06-2013
// ***********************************************************************
// <copyright file="DataRepositories.cs" company="Copyright © Innovacall 2011">
//     Copyright (c) Copyright © Innovacall 2011. All rights reserved.
// </copyright>
// <summary></summary>
// ***********************************************************************
using System;
using System.Collections.Generic;
using System.Data.Objects;
using System.Threading;
using InnovaApp.Layers.EntitiesLayer;
namespace InnovaApp.Layers.DataLayer
{
    /// <summary>
    /// Class DataRepositories
    /// </summary>
	public partial class DataRepositories : IDisposable
    {
        /// <summary>
        /// Class ObjectContextCache
        /// </summary>
        private static class ObjectContextCache
        {
            /// <summary>
            /// Initializes static members of the <see cref="ObjectContextCache"/> class.
            /// </summary>
            static ObjectContextCache()
            {
                _contextCache = new Dictionary<Thread, WeakReference>();
            }

            /// <summary>
            /// The _context cache
            /// </summary>
            private static Dictionary<Thread, WeakReference> _contextCache;

            /// <summary>
            /// Gets the context.
            /// </summary>
            /// <returns>ObjectContext.</returns>
            public static ObjectContext GetContext()
            {
                ObjectContext _context = null;
                _context = new InnovaAppEntities();
                return _context;
            }

            /// <summary>
            /// Clears the cache.
            /// </summary>
            private static void ClearCache()
            {
                List<Thread> _toRemove = new List<Thread>();
                foreach (Thread t in _contextCache.Keys)
                    if (!_contextCache[t].IsAlive)
                        _toRemove.Add(t);

                foreach (Thread t in _toRemove)
                    _contextCache.Remove(t);
            }
        }


        /// <summary>
        /// Initializes a new instance of the <see cref="DataRepositories"/> class.
        /// </summary>
        public DataRepositories()
        {
            _context = ObjectContextCache.GetContext();
        }

        /// <summary>
        /// The _context disposed
        /// </summary>
        bool _contextDisposed = false;

        /// <summary>
        /// Finalizes an instance of the <see cref="DataRepositories"/> class.
        /// </summary>
        ~DataRepositories()
        {
            if (!_contextDisposed)
                this.Dispose();
        }

        /// <summary>
        /// Exécute les tâches définies par l'application associées à la libération ou à la redéfinition des ressources non managées.
        /// </summary>
        public void Dispose()
        {
            _contextDisposed = true;
            _context = null;
            GC.SuppressFinalize(this);
        }
    }
}
